<template>
  <div class="yl-container">

    <h1 style="font-size:1.5em;text-align:center;">{{title}}</h1><br>

    <div class="yl-main">
      <!-- 搜索模块 -->
      <div class="yl-search-list">
        <el-form :model="searchConfig" ref="searchConfig" :inline="true">
          <el-form-item label="类型">
            <el-select v-model="searchConfig.category_id" placeholder="请选择类型" size="small">
              <el-option
                v-for="(item, index) in categoryList"
                :key="index"
                :value="item.nns_id"
                :label="item.nns_name"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <YlButton uiType="default" icon="el-icon-search" @click="initData(false)">搜 索</YlButton>
            <YlButton uiType="default" btnType="success" icon="iconfont yl-ecs-zhongzhi yl-icon" @click="initTable(false)">重 置</YlButton>
          </el-form-item>
        </el-form>

        <YlButton uiType="plain"  icon="el-icon-circle-plus-outline" @click="initAdd">新增</YlButton>
        <YlButton uiType="plain" btnType="danger" icon="el-icon-refresh-right" @click="initData(true)">强制刷新</YlButton>
        <YlButton uiType="plain"  icon="el-icon-download" @click="downloadFile">导出文件</YlButton>

      </div>
      <br>
      <!-- table 列表组 -->
      <div class="yl-table-list">
        <el-table
          v-loading="loading"
          element-loading-text="玩命加载中"
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.8)"
          height="550"
          :data="tableData"
          show-summary
        >
          <el-table-column align="center" label="处理时间" prop="nns_occurrence_time" show-overflow-tooltip></el-table-column>

          <el-table-column label="单类" align="center" prop="nns_real_category_name"></el-table-column>

          <el-table-column align="center" label="单号" prop="nns_category_num" ></el-table-column>

          <el-table-column align="center" label="摘要" prop="nns_summary" show-overflow-tooltip></el-table-column>

          <el-table-column align="center" label="经办人" prop="nns_operator" ></el-table-column>

          <el-table-column align="center" label="发生金额(元)">
            <template slot-scope="{row}">
              <span>{{ row.nns_amount_money | formatMoney() }}</span>
            </template>
          </el-table-column>

          <el-table-column align="center" label="实际发生金额(元)">
             <template slot-scope="{row}">
              <span>{{ row.nns_actual_amount_money | formatMoney() }}</span>
            </template>
          </el-table-column>

          <el-table-column align="center" label="填报人" prop="nns_uploader_name"></el-table-column>

          <el-table-column align="center"  label="操作" fixed="right" width="200">
            <template slot-scope="scope">
              <YlButton uiType="main" icon="el-icon-view" v-show="query.canWrite" @click="handleShowSingle(scope.row)">编辑</YlButton>
              <YlButton uiType="main" icon="el-icon-delete" btnType="danger" v-show="query.canWrite" @click="del(scope.row)">删除</YlButton>
            </template>
          </el-table-column>

          <div class="yl-table-empty" slot="empty">
            <img src="~@/assets/icons/svg/no-data.svg">
            <div>暂无数据</div>
          </div>
        </el-table>
      </div>
    </div>
    <el-dialog class="yl-dialog" :title="formConfig.isFix ? '修改类型' : '新增类型'" :visible.sync="dialogVisible">
      <YlForm
          ref="ylform"
          :formConfig="formConfig"
          @close-dialog="dialogVisible = false"
          @init-state="getInitState"
        />
    </el-dialog>
  </div>
</template>

<script>
const Base64 = require('js-base64').Base64
export default {
  data () {
    return {
      categoryList: [],
      canWrite: false,
      dialogVisible: false,
      formConfig: {
        inline: false,
        changeId: '',
        isFix: false,
        formData: {
          stage_id: '',
          category_id: '',
          real_category_name: '',
          category_num: '',
          occurrence_time: '',
          operator: '',
          amount_money: '',
          actual_amount_money: '',
          summary: '',
          remark: ''
        },
        editRules: {
          real_category_name: [
            { required: true, message: '请输入单类名称', trigger: 'blur' }
          ],
          category_num: [
            { required: true, message: '请输入单号', trigger: 'blur' }
          ],
          occurrence_time: [
            { required: true, message: '请输入经办时间', trigger: 'blur' }
          ],
          operator: [
            { required: true, message: '请输入经办人', trigger: 'blur' }
          ],
          amount_money: [
            { required: true, message: '请输入发生金额', trigger: 'blur' }
          ],
          actual_amount_money: [
            { required: true, message: '请输入实际发生金额', trigger: 'blur' }
          ],
          summary: [
            { required: true, message: '请输入摘要', trigger: 'blur' }
          ]
        },
        columns: [
          {
            type: 'input',
            label: '单类名称',
            prop: 'real_category_name'
          },
          {
            type: 'input',
            label: '单号',
            prop: 'category_num'
          },
          {
            type: 'time',
            label: '经办时间',
            prop: 'occurrence_time'
          },
          {
            type: 'input',
            label: '经办人',
            prop: 'operator'
          },
          {
            type: 'input',
            label: '发生金额',
            prop: 'amount_money'
          },
          {
            type: 'input',
            label: '实际发生金额',
            prop: 'actual_amount_money'
          },
          {
            type: 'textarea',
            label: '摘要',
            prop: 'summary'
          },
          {
            type: 'textarea',
            label: '备注',
            prop: 'remark'
          }
        ],
        addApi: this.$ylapi.addContentOffBudget,
        changeApi: this.$ylapi.changeContentOffBudget
      },
      loading: false,
      // 标题
      title: '',
      // 下载地址
      fileUrl: '',
      // table 列表数据
      tableData: [],
      searchConfig: {
        category_id: ''
      },
      // 分页
      page: 1,
      size: 10,
      total: 0,
      // query信息
      query: {}
    }
  },
  created () {
    this.query = this.$route.query
    this.formConfig.formData.stage_id = this.query.id
    this.initTable()
  },
  methods: {
    // 新增初始化
    initAdd () {
      this.dialogVisible = true
      // 初始化formdata
      this.$nextTick(() => this.$refs['ylform'].$refs['form'].resetFields())
      this.formConfig.formData.category_id = this.searchConfig.category_id
      this.formConfig.btnMethod = ['submit', 'cancel']
    },
    // 列表初始化
    initTable (isForce = false) {
      this.loading = true
      this.dialogVisible = false
      this.initCategory()
      this.initData(isForce)
      this.formConfig.formData.stage_id = this.query.id
    },

    async initCategory () {
      let data = await this.$ylapi.getCategorySelectOffBuddget({
        'stage_id': this.query.id
      })
      this.categoryList = data
    },

    // 数据初始化
    async initData (isForce = false) {
      let data = await this.$ylapi.getContentListOffBuddget({
        'stage_id': this.query.id,
        'category_id': this.searchConfig.category_id,
        'refresh': isForce
      })
      this.loading = false
      this.title = data.title
      this.fileUrl = data.file_url
      this.tableData = data.data
      this.searchConfig.category_id = data.category_id
    },

    // 编辑回显
    handleShowSingle (row) {
      this.formConfig.changeId = row.nns_id
      this.formConfig.isFix = true
      this.formConfig.btnMethod = ['save', 'cancel']
      this.formConfig.formData = this.assignForm(this.formConfig.formData, row)
      this.dialogVisible = true
    },

    // 删除
    async del ({ nns_id }) {
      // eslint-disable-next-line no-unused-expressions
      await this.$modal.confirm(this, {
        api: this.$ylapi.deleteContentOffBudget,
        obj: nns_id
      })
        ? this.initTable(true)
        : ''
    },

    // 导出文档--二进制流
    // async downloadFile () {
    //   let { data, name } = await this.$ylapi.downloadFile({
    //     url: this.fileUrl,
    //     mode: 'json'
    //   })
    //   this.blobFile(Base64.decode(data), name)
    // },

    // 导出文档--location
    downloadFile () {
      this.locateFile(`/api/resource/download?url=` + this.fileUrl)
    },

    // 跳转
    goAddCategory (canWrite, id) {
      this.$router.push({
        path: `/addFill`,
        query: {
          id,
          canWrite
        }
      })
    },
    // 分页---页容量选择器
    handlePageSize (val) {
      this.size = val
      this.loading = true
      this.initData()
    },
    // 分页
    handleCurrentPage (val) {
      this.page = val
      this.loading = true
      this.initData()
    },
    // 初始化
    getInitState (val) {
      if (val) {
        this.initTable(true)
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
